/**
 * <p><b>Title:</b> ContentBlackController.java </p>
 * @Package com.wondertek.poms.image.controller
 */
package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IProductPropService;
import com.wondertek.poms.dao.po.ProductProp;
import com.wondertek.poms.dao.pojo.ProductPropPojo;
import com.wondertek.poms.dao.vo.ProductPropVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.groups.Default;
import java.util.List;

/**
 * <p><b>ContentBlackController Description:</b> 产品属性表Controller层实现</p>
 * @author 窦再兴
 * <p><b>DATE</b> 2020-12-03 10:19:26</p>
 */
@Api(value="ContentBlackController", description="产品属性接口")
@RestController
@Scope("prototype")
@RequestMapping(value="/product-prop")
@Slf4j
@Validated
public class ProductPropController extends BaseController {
	
	@Autowired
	private IProductPropService productPropService;
	
	@ApiOperation(value="产品属性列表", notes="产品属性列表")
	@GetMapping("/list")
	@ResponseBody
	public ResultBean list(@Valid  ProductPropVo productPropVo,
			@RequestParam(defaultValue="1") int page, 
			@RequestParam(defaultValue="20") int rows,
			@RequestParam(defaultValue="UPDATE_TIME") String sidx,
			@RequestParam(defaultValue="desc") String sord) {
		Page<ProductPropVo> pages = productPropService.findAll(productPropVo, page, rows, sord, sidx);
		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}

	@ApiOperation(value="产品属性新增", notes="产品属性新增")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "contentId", required = true),
		@ApiImplicitParam(name = "packageId", required = true),
		@ApiImplicitParam(name = "prdInfoId", required = true),
	})
	@PostMapping(value = "/save")
	@ResponseBody
	public ResultBean add(@Valid  @RequestBody ProductPropPojo productPropVo) {
		ProductProp productProp = new ProductProp();
		productProp.setPropValue(productPropVo.getPropValue());
		List<ProductProp> list=productPropService.findAll(productProp);
		if(list!=null && list.size()>0){
			return ResultBean.error("属性值重复");
		}
		productProp = new ProductProp();
		productProp.setProductPropName(productPropVo.getProductPropName());
		list=productPropService.findAll(productProp);
		if(list!=null && list.size()>0){
			return ResultBean.error("属性名重复");
		}
		BeanUtils.copyProperties(productPropVo, productProp);
		if(productPropVo.getId()==null){
			ProductProp.putTimeAndUser(productProp);
			ProductProp.updateTimeAndUser(productProp);
		}else{
			ProductProp.updateTimeAndUser(productProp);
		}
		return PageBean.ok(productPropService.save(productProp));
	}
	

	@ApiOperation(value="产品属性删除", notes="产品属性删除")
	@DeleteMapping(value = "/del")
	@ResponseBody
	public ResultBean delete(@Valid @NotBlank(message = "产品id不能为空", groups = {Default.class})
								 @RequestParam String ids) {
		String[] idd=ids.split(",");
		for (String id:idd) {
			productPropService.deleteById(Long.parseLong(id.trim()));
		}
		return ResultBean.ok();
	}
}
